package com.kgc.controller;

import com.kgc.annotation.CheckPermission;
import com.kgc.entity.Category;
import com.kgc.service.CategoryService;
import com.kgc.util.ResultAjax;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @RequestMapping("/page")
    public ResultAjax categoryPage(@RequestBody Map<String, String> params) {
        int pageNum = Integer.parseInt(params.get("pageNum"));
        int pageSize = Integer.parseInt(params.get("pageSize"));
        String searchName = params.get("searchName");
        int searchType = Integer.parseInt(params.get("searchType"));
        Map<String, Object> paramsPage = new HashMap<>();
        if (!StringUtils.isEmpty(searchName)) {
            paramsPage.put("searchName", searchName);
        }
        if (searchType != 0) {
            paramsPage.put("searchType", searchType);
        }
        paramsPage.put("pageNum", pageNum);
        paramsPage.put("pageSize", pageSize);
        return ResultAjax.success().add("pageInfo", categoryService.selectCategoryBage(paramsPage));
    }

    @RequestMapping("/getTypes1")
    public ResultAjax getTypes1() {
        List<Category> list = categoryService.getType1Category();
        System.out.println(list);
        return ResultAjax.success().add("list", list);
    }

    @RequestMapping("/getTypes2")
    public ResultAjax getType2(@RequestBody Map<String, String> params) {
        int id = Integer.parseInt(params.get("id"));
        List<Category> list = categoryService.getType2Category(id);
        return ResultAjax.success().add("list", list);
    }

    @RequestMapping("/getTypes3")
    public ResultAjax getType3(@RequestBody Map<String, String> params) {
        int id = Integer.parseInt(params.get("id"));
        List<Category> list = categoryService.getType3Category(id);
        return ResultAjax.success().add("list", list);
    }

    @RequestMapping("/checkName")
    public ResultAjax checkCategoryByName(@RequestBody Map<String, String> params) {

        String name = params.get("name");
        int type = Integer.parseInt(params.get("type"));
        Category category = categoryService.getCategoryByName(name, type);
        if (category != null) {
            return ResultAjax.success().add("result", "exist");
        }
        return ResultAjax.success().add("result", "not exist");
    }


    @RequestMapping("/addCategory")
    @CheckPermission("1")
    public ResultAjax addCategory(@RequestBody Map<String, String> params) {
        String name = params.get("name");
        int type = Integer.parseInt(params.get("type"));
        int parentId = Integer.parseInt(params.get("parentId"));
        Category category = new Category();
        category.setName(name);
        category.setParentId(parentId);
        category.setType(type);
        if (categoryService.addCategory(category) > 0) {
            return ResultAjax.success().add("result", "添加成功");
        }
        return ResultAjax.success().add("result", "添加失败");
    }

    @RequestMapping("/deleteCategory")
    @CheckPermission("1")
    public ResultAjax deleteCategory(@RequestBody Map<String, String> params) {
        int id = Integer.parseInt(params.get("id"));
        if (categoryService.getProductByCategoryId(id) > 0) {
            return ResultAjax.success().add("result", "该商品分类下还有商品，不可删除");
        }
        if (categoryService.getSonCountByCategoryId(id) > 0) {
            return ResultAjax.success().add("result", "该商品分类下还存在其他商品分类，不可删除");
        }
        if (categoryService.deleteCategory(id) > 0) {
            return ResultAjax.success().add("result", "删除成功");
        }
        return ResultAjax.success().add("result", "删除失败");
    }


    @RequestMapping("/getCategory")
    public ResultAjax getCategoryById(@RequestBody Map<String, String> params) {
        int id = Integer.parseInt(params.get("id"));
        Category category = categoryService.getCategoryById(id);
        return ResultAjax.success().add("category", category);
    }

    @RequestMapping("/updateCategory")
    public ResultAjax updateCategory(@RequestBody Map<String, String> params) {
        int id = Integer.parseInt(params.get("id"));
        int parentId = Integer.parseInt(params.get("parentId"));
        String name = params.get("name");
        int type = Integer.parseInt(params.get("type"));
        Category category = categoryService.getCategoryById(id);
        category.setName(name);
        category.setParentId(parentId);
        category.setType(type);
        if (categoryService.updateCategory(category) > 0) {
            return ResultAjax.success().add("result", "修改成功");
        }

        return ResultAjax.success().add("result", "修改失败");
    }


    @RequestMapping("/checkName2")
    public ResultAjax checkName1(@RequestBody Map<String, String> params) {
        String name = params.get("name");
        int id = Integer.parseInt(params.get("id"));
        int type = Integer.parseInt(params.get("type"));
        Category category = categoryService.getCategoryByName(name, type);
        if (category != null) {
            if (categoryService.getCategoryById(id).getName().equals(name)) {
                return ResultAjax.success().add("result", "not exist");
            }
            return ResultAjax.success().add("result", "exist");
        }
        return ResultAjax.success().add("result", "not exist");
    }


    @RequestMapping("/pageInfoCategory")
    @CheckPermission("1")
    public ResultAjax pageInfoCategory() {
        return ResultAjax.success().add("categoryList", categoryService.getAllCategoryAndChild());
    }


}
